import pandas as pd
class performance:
    def __init__(self,orders,returns):
        self.orders=pd.DataFrame(orders["byid"]).T
        self.orders=self.orders[self.orders["tradedqty"]!=0]
        self.return_asset=returns
        self.returns=pd.DataFrame(returns)
        self.returns.index=self.returns["timekey"]
        self.tax=self.orders["tax"].sum()
        self.tradecount=len(self.orders)
        self.year_tradedays=252
        self.year_tradeweekdays = 52
        self.year_trademonths = 12
        self.year_tradehour = self.year_tradedays*6

        self.months=len(self.returns["return"].resample("1M").sum())
        self.weekdays=len(self.returns["return"].resample("1W").sum())
        self.days=len(self.returns["return"].resample("1D").sum())
        self.hours=len(self.returns["return"].resample("1H").sum())

        self.maxdrawdown=None
    def calc_tax(self,orders):
        return self.orders["tax"].sum()
    def return_sheet(self,period=None,free_return=0.5):
        sheet={}
        if not period:
            r=self.return_asset.assets
        else:
            r=self.return_asset.convert(period)
        r_df = pd.DataFrame(r)
        r_df.index = r_df["timekey"]
        sheet["绝对收益"]=(r[-1]["asset"]-r[0]["asset"])/r[0]["asset"]
        sheet["相对收益"] =r[-1]["asset"]-r[0]["asset"]
        sheet["累计收益"] =(r_df["return"]+1).prod()-1
        sheet["年化收益"] =(r[-1]["asset"]/r[0]["asset"])**(self.year_tradedays/self.days)-1
        sheet["月化收益"] =(r[-1]["asset"]/r[0]["asset"])**(self.year_trademonths/self.months)-1
        sheet["周化收益"] =(r[-1]["asset"]/r[0]["asset"])**(self.year_tradeweekdays/self.weekdays)-1
        sheet["日化收益"] =(r[-1]["asset"]/r[0]["asset"])**(self.year_tradedays/self.days)-1
        sheet["总税费"] =self.tax
        sheet["收益费比"] =sheet["相对收益"] /sheet["总税费"]
        dr=r_df["return"]+1
        dr1=dr.resample("1D").prod()-1
        sheet["收益波动-日"] =dr1.std()
        dr2=dr.resample("1H").prod()-1
        sheet["收益波动-时"] =dr2.std()
        sheet["夏普比率（Sharp"]=(sheet["年化收益"]-free_return)/sheet["收益波动-日"]
        draw=self.calc_drawdown(period=period)
        d0=pd.DataFrame(draw).T
        key=d0["pl"].astype("float64").idxmin().to_pydatetime()
        info=draw[key]
        sheet["最大回撤"]=info["pl"]
        sheet["最大回撤持续期"] =info["downdays"]
        sheet["交易次数"]=self.tradecount
    def calc_drawdown(self,returns=None,period=None):
        r0=self.return_asset
        if returns:
            r0=returns
        r=r0.assets
        if period:
            r=r0.convert(period)
        max_a=0
        drawdown={}
        count=0
        for i in r:
            key=i["timekey"]
            if max_a<i["asset"]:
                max_a=i["asset"]
                count=0
            else:
                count=count+1
            a=i["asset_limit"]
            t={}
            t["drawdown"]=max_a-a
            t["downdays"]=count
            drawdown[key]=t
        return drawdown
    def report(self,bars,name=None):
        pass


